package com.order.controller.user;


import com.order.dto.CommentsDto;
import com.order.dto.ProductPageQueryDTO;
import com.order.result.PageResult;
import com.order.result.Result;
import com.order.service.CommentsService;
import com.order.service.ProductService;
import com.order.vo.CommentsVo;
import com.order.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "用户-乐器相关接口")
@RestController("userProductController")
@RequestMapping("/user/product")
public class ProductController {

    @Autowired
    private ProductService productService;
    @Autowired
    private CommentsService commentsService;

    @ApiOperation("根据id查询乐器")
    @GetMapping("/{id}")
    public Result<ProductVo> getById(@PathVariable Long id){
        log.info("查找乐器id为:{}", id);
        ProductVo vo = productService.getBydId(id);
        return Result.success(vo);
    }

    @ApiOperation("乐器分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(ProductPageQueryDTO pageQueryDTO){
        PageResult pageResult = productService.PageQuery(pageQueryDTO);
        return Result.success(pageResult);
    }

    @ApiOperation("用户评论")
    @PostMapping("/comments")
    public Result comments(@RequestBody CommentsDto commentsDto){
        commentsService.insert(commentsDto);
        return Result.success();
    }


    @ApiOperation("查看评论")
    @GetMapping("/comments/{productId}")
    public Result<List<CommentsVo>> comments(@PathVariable Long productId){
        List<CommentsVo> comments = commentsService.search(productId);
        return Result.success(comments);
    }

}
